import { useEffect, useState } from 'react';
import getHello from '../services/helloService';

const services = {
  getHello,
} as const;

export type ServiceName = keyof typeof services;

export function useService(serviceName: ServiceName, options?: RequestInit) {
  const [data, setData] = useState();
  const [isLoading, setIsLoading] = useState(true);
  const service = services[serviceName];
  useEffect(() => {
    async function callService() {
      setData((await service(options)) as any);
      setIsLoading(false);
    }

    callService();
  }, []);

  return { data, isLoading };
}
